/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package HomeWork7Threads;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Jakim
 */
public class ThreadApplet extends Applet {

    //private fields.
    private Label lblThread1;
    private Label lblThread2;
    private Label lblStep1;
    private Label lblStep2;
    private TextField txtThread1;
    private TextField txtThread2;
    private Button btnStartThread1;
    private Button btnStopThread1;
    private Button btnStartThread2;
    private Button btnStopThread2;
    private Choice cbThread1Step;
    private Choice cbThread2Step;
    private Panel panelThread1;
    private Panel panelThread2;

    SpinThread thread1;
    SpinThread thread2;

    // Consturctor
    public ThreadApplet(){
        this.panelThread1 = new Panel();
        this.panelThread1.setLayout(new GridLayout(4, 2));
        this.panelThread2 = new Panel();
        this.panelThread2.setLayout(new GridLayout(4, 2));

        this.lblThread1 = new Label("Thread1");
        this.lblStep1 = new Label("Step: ");
        this.lblThread2 = new Label("Thread2");
        this.lblStep2 = new Label("Step: ");

        this.txtThread1 = new TextField("");
        this.txtThread2 = new TextField("");

        this.btnStartThread1 = new Button("Start");
        this.btnStopThread1 = new Button("Stop");
        this.btnStartThread2 = new Button("Start");
        this.btnStopThread2 = new Button("Stop");

        this.cbThread1Step = new Choice();
        this.cbThread1Step.add("1");
        this.cbThread1Step.add("5");
        this.cbThread1Step.add("10");
        this.cbThread1Step.select(0);
        this.cbThread2Step = new Choice();
        this.cbThread2Step.add("1");
        this.cbThread2Step.add("5");
        this.cbThread2Step.add("10");
        this.cbThread2Step.select(0);

        this.panelThread1 = new Panel();
        this.panelThread1.setLayout(new GridLayout(4, 2));
        this.panelThread1.add(this.lblThread1);
        this.panelThread1.add(this.txtThread1);
        this.panelThread1.add(this.lblStep1);
        this.panelThread1.add(this.cbThread1Step);
        this.panelThread1.add(this.btnStartThread1);
        this.panelThread1.add(this.btnStopThread1);

        this.panelThread2 = new Panel();
        this.panelThread2.setLayout(new GridLayout(4, 2));
        this.panelThread2.add(this.lblThread2);
        this.panelThread2.add(this.txtThread2);
        this.panelThread2.add(this.lblStep2);
        this.panelThread2.add(this.cbThread2Step);
        this.panelThread2.add(this.btnStartThread2);
        this.panelThread2.add(this.btnStopThread2);
    }

    @Override
    public void init() {
        this.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
        this.add(this.panelThread1);
        this.add(this.panelThread2);
        this.thread1 = new SpinThread(this.txtThread1, this.cbThread1Step);
        this.thread2 = new SpinThread(this.txtThread2, this.cbThread2Step);
    }

    @Override
    public void start() {
        this.setVisible(true);
        this.btnStartThread1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (!thread1.isAlive()){
                    thread1 = new SpinThread(txtThread1, cbThread1Step);
                }
                thread1.start();
            }
        });

        this.btnStartThread2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (!thread2.isAlive()){
                    thread2 = new SpinThread(txtThread2, cbThread2Step);
                }
                thread2.start();
            }
        });
        this.btnStopThread1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                thread1.interrupt();
                
            }
        });
        this.btnStopThread2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                thread2.interrupt();
            }
        });
    }
}
